package com.jy.adscm.hcPlan.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hcPlan.bean.Hc_plan_materiel;
import com.jy.adscm.hcPlan.bean.Hc_plan_supplier;
import com.jy.adscm.hcPlan.bean.Hc_plan_supplier_materiel;
import com.jy.adscm.hcPlan.mapper.mysql.Hc_plan_materielMapper;
import com.jy.adscm.hcPlan.mapper.mysql.Hc_plan_supplierMapper;
import com.jy.adscm.hcPlan.mapper.mysql.Hc_plan_supplier_materielMapper;
import com.jy.adscm.hcPlan.service.Hc_plan_materielService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 物料清单 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-09-11
 */
@Service
public class Hc_plan_materielServiceImpl extends ServiceImpl<Hc_plan_materielMapper, Hc_plan_materiel> implements Hc_plan_materielService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    Hc_plan_materielMapper hc_plan_materielMapper;
    @Autowired
    Hc_plan_supplierMapper hc_plan_supplierMapper;
    @Autowired
    Hc_plan_supplier_materielMapper hc_plan_supplier_materielMapper;

    @Override
    public List<Hc_plan_materiel> queryAllMaterielList() throws AdscmSystemException {

        List<Hc_plan_materiel> list = null;
        logger.info("查询所有物料清单...");
        try {
            list = hc_plan_materielMapper.queryAllMaterielList();
        }catch (Exception e){
            logger.error("查询所有物料清单异常",e);
            throw new AdscmSystemException(e.getMessage());
        }

        return list;
    }

    @Transactional
    @Override
    public void addMaterielConfig(Hc_plan_materiel materiel) throws AdscmSystemException{
        logger.info("增加物料标准配置...");
        try {
            hc_plan_materielMapper.insert(materiel);
            List<Hc_plan_supplier> suppliers = materiel.getSuppliers();

           if (suppliers != null && suppliers.size()>0){
               for (Hc_plan_supplier supplier :suppliers){
                   Hc_plan_supplier_materiel sm = new Hc_plan_supplier_materiel();
                   sm.setMateriel_id(materiel.getId());
                   sm.setSupplier_id(supplier.getId());
                   hc_plan_supplier_materielMapper.insert(sm);
               }
           }
        }catch (Exception e){
            logger.error("增加物料标准配置异常",e);
            throw new AdscmSystemException(e.getMessage());
        }
    }

    @Override
    public void editMaterielConfig(Hc_plan_materiel materiel) throws AdscmSystemException{
        logger.info("编辑物料标准配置...");
        try {
            hc_plan_materielMapper.updateById(materiel);
            List<Hc_plan_supplier> suppliers = materiel.getSuppliers();
            if (suppliers != null && suppliers.size()>0){
                Wrapper<Hc_plan_supplier_materiel> wrapper = new EntityWrapper<>();
                wrapper.eq("materiel_id",materiel.getId());
                hc_plan_supplier_materielMapper.delete(wrapper);
                for (Hc_plan_supplier supplier :suppliers){
                    Hc_plan_supplier_materiel sm = new Hc_plan_supplier_materiel();
                    sm.setMateriel_id(materiel.getId());
                    sm.setSupplier_id(supplier.getId());
                    hc_plan_supplier_materielMapper.insert(sm);
                }
            }

        }catch (Exception e){
            logger.error("编辑物料标准配置异常",e);
            throw new AdscmSystemException(e.getMessage());
        }
    }

    @Override
    public void delMaterielConfig(Integer id) throws AdscmSystemException {
        logger.info("删除物料标准配置...");
        try {
            Hc_plan_materiel materiel = hc_plan_materielMapper.selectById(id);
            List<Hc_plan_supplier> suppliers = materiel.getSuppliers();
            if (suppliers != null && suppliers.size()>0){
                Wrapper<Hc_plan_supplier_materiel> wrapper = new EntityWrapper<>();
                wrapper.eq("materiel_id",materiel.getId());
                hc_plan_supplier_materielMapper.delete(wrapper);
            }
            hc_plan_materielMapper.deleteById(materiel);

        }catch (Exception e){
            logger.error("删除物料标准配置异常",e);
            throw new AdscmSystemException(e.getMessage());
        }
    }


    @Override
    public List<Hc_plan_supplier> getAllHc_plan_supplier() throws AdscmSystemException {
        logger.info("查询所有物料供应商列表...");
        return hc_plan_supplierMapper.selectList(null);
    }
}
